package com.nf.health.app.sport_recipe.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hyphenate.easeui.widget.photoview.EasePhotoView;
import com.hyphenate.easeui.widget.photoview.PhotoViewAttacher;
import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.core.MyGsonBuilder;
import com.nf.health.app.http.Act;
import com.nf.health.app.sport_recipe.bean.HouseMindBean;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.List;

/**
 * 心藏功能检测报告
 * @author changpeng
 */
public class HouseMindActivity extends BaseActivity {
    private String mImgUrl ="";
    private EasePhotoView mPV;
    private TextView mTitlebarTitleTv;
    private static final String IMG_ATT = "resultimg";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.layout_common_header);
        setContent(R.layout.activity_display_image);
        mTitlebarTitleTv = (TextView) findViewById(R.id.titlebar_title_tv);
        mPV = (EasePhotoView) findViewById(R.id.image);
        mPV.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
            @Override
            public void onPhotoTap(View view, float x, float y) {
                finish();
            }
        });
        mPV.setMaxScale(3.0f);
        mTitlebarTitleTv.setText("心藏功能检测报告");
        initData();
    }

    private void initData(){
        provider.getDeviceDataByType(Constants.TYPE_HM);
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        if(Act.Flag.FLAG_GET_DEVICEDATABYTYPE.equals(actionName)){
            Gson gson = new MyGsonBuilder().createGson();
            List<HouseMindBean> beanList = gson.fromJson(object+"", new TypeToken<List<HouseMindBean>>() {
            }.getType());
            //由于json结构的关系，需遍历出resultimg字段，取该字段的值
            for (int i = 0; i < beanList.size(); i++) {
                HouseMindBean bean = beanList.get(i);
                if(IMG_ATT.equals(bean.getColumn())){
                    mImgUrl = bean.getValue();
                }
            }
        }
        ImageLoader.getInstance().displayImage(mImgUrl, mPV, new DisplayImageOptions.Builder()
                .bitmapConfig(Bitmap.Config.ARGB_8888)//为了能让用户看清楚图片，所以质量要求高些
                .cacheInMemory(true)
                .cacheOnDisc(true)
                .imageScaleType(ImageScaleType.NONE)
                .cacheOnDisc(true)
                .build());
        mPV.zoomTo(2f,2f,2f);
    }

    @Override
    public void handleActionError(String actionName, String response, VolleyError error) {
        super.handleActionError(actionName, response, error);
    }
}
